package com.example.njupt;

import android.content.Intent;
import android.widget.FrameLayout;

import androidx.fragment.app.FragmentTransaction;

import com.example.njupt.base.BaseActivity;
import com.example.njupt.constant.AppStatusConstant;
import com.example.njupt.helper.StatusBarHelper;
import com.example.njupt.log.NjuptLogger;
import com.example.njupt.okhttp.entity.TabEntity;
import com.example.njupt.uis.main.MainFragment;
import com.example.njupt.uis.mine.MineFragment;
import com.example.njupt.uis.reservation.ReservationFragment;
import com.example.njupt.uis.task.TaskFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.vp_main)
    FrameLayout viewPager;
    @BindView(R.id.tab_main)
    CommonTabLayout tabMain;

    private List<String> mTitle = new ArrayList<>();
    private ArrayList<CustomTabEntity> mEntity = new ArrayList<>();
    private List<Integer> mIcon = new ArrayList<>();
    private List<Integer> mIconSelect = new ArrayList<>();
    private MainAdapter mAdapter;
    private int mCurrentIndex = 0;

    private MainFragment mainFragment;
    private ReservationFragment reservationFragment;
    private TaskFragment taskFragment;
    private MineFragment mineFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTitle.add("首页");
        mTitle.add("关注");
        mTitle.add("报告");
        mTitle.add("我的");

        mIcon.add(R.mipmap.ic_main_uns);
        mIcon.add(R.mipmap.ic_reservation_uns);
        mIcon.add(R.mipmap.ic_task_uns);
        mIcon.add(R.mipmap.ic_mine_uns);

        mIconSelect.add(R.mipmap.ic_main_s);
        mIconSelect.add(R.mipmap.ic_reservation_s);
        mIconSelect.add(R.mipmap.ic_task_s);
        mIconSelect.add(R.mipmap.ic_mine_s);

        initTab();
        tabMain.postDelayed(() -> tabMain.setCurrentTab(mCurrentIndex), 100);
        switchFragment(mCurrentIndex);
        switchStatusBar(mCurrentIndex);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int action = intent.getIntExtra(AppStatusConstant.KEY_HOME_ACTION, AppStatusConstant.ACTION_BACK_TO_HOME);
        if (action == AppStatusConstant.ACTION_RESTART_APP) {
            protectApp();
        }
    }

    @Override
    protected void protectApp() {
        startActivity(new Intent(MainActivity.this, SplashActivity.class));
        finish();
    }

    private void initTab() {
        for (int i = 0; i < mTitle.size(); i++) {
            mEntity.add(new TabEntity(mTitle.get(i), mIconSelect.get(i), mIcon.get(i)));
        }
        tabMain.setTabData(mEntity);
        tabMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchStatusBar(position);
                switchFragment(position);
                NjuptLogger.instance.e(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void switchStatusBar(int position) {
        StatusBarHelper.getInstance().setStatusBarFontColor(getWindow(), position != 3);
    }

    public void switchFragment(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (position) {
            case 1:
                if (reservationFragment == null) {
                    reservationFragment = new ReservationFragment();
                    transaction.add(R.id.vp_main, reservationFragment, "reservation");
                } else {
                    transaction.show(reservationFragment);
                }
                break;
            case 2:
                if (taskFragment == null) {
                    taskFragment = new TaskFragment();
                    transaction.add(R.id.vp_main, taskFragment, "task");
                } else {
                    transaction.show(taskFragment);
                }
                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.vp_main, mineFragment, "mine");
                } else {
                    transaction.show(mineFragment);
                }
                break;
            default:
                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                    transaction.add(R.id.vp_main, mainFragment, "main");
                } else {
                    transaction.show(mainFragment);
                }
                break;
        }
        mCurrentIndex = position;
        tabMain.setCurrentTab(mCurrentIndex);
        transaction.commitAllowingStateLoss();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (null != mainFragment) {
            transaction.hide(mainFragment);
        }
        if (null != reservationFragment) {
            transaction.hide(reservationFragment);
        }
        if (null != taskFragment) {
            transaction.hide(taskFragment);
        }
        if (null != mineFragment) {
            transaction.hide(mineFragment);
        }
    }
}